<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
*T_User
*Description: model manage users
*@author quanhm
*/

Class T_users extends MY_Model{

	function __construct() { 
		parent::__construct();

		$this->table_name = T_USERS;
	}

/**
 * get user by id
 * @param  string  $select : list fields property of user
 * @param  integer $id     : id of user
 * @return mix
 *         -1 if id is null
 *         0 if no user by id
 *         array select
 */
	function get_data_by_id($id = -1, $select = '*'){
		//validate data
		if(is_numeric($id) && $id != -1){

			$this->db->where(USER_ID ." = $id LIMIT 1");
		}else{

			return null;
		}

		//select data
		if(strcmp($select, '*') != 0){

			$this->db->select($select);
		}else{

			$this->db->select();
		}

		//query db
		$query= $this->db->get($this->table_name);

		$query = $query->result_array();

		//return result
		if(empty($query)){

			return -1;
			
		}else{

			return $query[0];	
		}
	}

/**
*function get_data_by_property
*
*Get data by property
*@param: 
*	param 1 : field list to take
*	param 2 : condition
*@return array|null
*@author quanhm
*
**/
	function get_data_by_property($select, $where = array()){
		//validate data
		if(!is_null($where) && is_array($where)){
			$this->db->where($where);
		}else{
			return null;
		}

		//select data
		if(strcmp($select, '*') != 0){
			$this->db->select($select);
		}else{
			$this->db->select();
		}
		
		//query
		$query= $this->db->get($this->table_name);

		//return result
		return $query->result_array();
	}


/**
*function set_data
*
*insert data tables user
*@param: 
*	param 1 : array data t_attractions_update
*	param 2 : 0|1
*@return default return id if $result = 0 | return user by id if $result = 1
*@author quanhm
*
**/
	function set_data($data = array(), $result = 0){
		if(is_null($data) || !is_array($data))
		{
			return null;
		}
		
		//insert data
		$this->db->insert($this->table_name, $data);

		//get last id
		$id_last = $this->db->insert_id();

		//return result
		if($result == 0){
			return $id_last;
		}else{
			$user = $this->get_data_by_id('*', $id_last);
			return $user;
		}
	}
	
/**
*function update_data_by_id
*
*Update user by id
*@param: 
*	param 1 : array data user
*	param 2 : id user
*@return true|false
*@author quanhm
*
**/
	function update_data_by_id($data = array(), $id = null){
		//validate data
		if(is_null($data) || !is_array($data)){
			return null;
		}

		//get data by id
		$this->db->where(USER_ID, $id);

		//update
		$this->db->update($this->table_name, $data);
		
		//return result
		if($this->db->affected_rows() > 0){
			return true;
		}else{
			return false;
		}	
	}
/**
*function update_data_by_property
*
*Update user by property
*@param: 
*	param 1 : array data user
*	param 2 : condition
*@return true|false
*@author quanhm
*
**/
	function update_data_by_property ($data = array(), $where = array()){
		//validate data
		if(is_null($data) || !is_array($data)){
			return null;
		}

		if(is_null($where) || !is_array($where)){
			return null;
		}

		//get data by condition where
		$this->db->where($where);

		//update data
		$this->db->update($this->table_name, $data);

		//return result
		if($this->db->affected_rows() > 0){
			return true;
		}else{
			return false;
		}

	}
}
